import java.sql.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

/**
 * 校园卡管理系统-版本二
 */
public class Load extends JFrame implements ActionListener {
    TextField t1, t2;//文本框
    JLabel p1, p2;//标签
    JButton b1, b2, b3, b4, b5, b6;//按钮
    JTextArea ta, ta1;//文本区

    //构造函数，所有控件的设计
    Load() {
        this.setTitle("用户登陆！");
        p1 = new JLabel("请输入帐号：");
        p2 = new JLabel("请输入密码：");

        t1 = new TextField(10);
        t2 = new TextField(10);
        t2.setEchoChar('*');//回显字

        ta = new JTextArea(10, 20);
        ta.setBackground(Color.cyan);
        ta1 = new JTextArea(10, 20);
        ta1.setBackground(Color.cyan);

        b1 = new JButton("确定");
        b2 = new JButton("退出");
        b3 = new JButton("查询信息");
        b4 = new JButton("修改密码");
        b5 = new JButton("增加成员");
        b6 = new JButton("删除成员");

        t1.addActionListener(this);
        t2.addActionListener(this);

        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        b4.addActionListener(this);
        b5.addActionListener(this);
        b6.addActionListener(this);

        Container c = getContentPane();//把组键放到容器中
        c.setLayout(null);//空布局

        c.add(p1);
        c.add(p2);

        c.add(t1);
        c.add(t2);

        c.add(ta);
        c.add(ta1);

        c.add(b1);
        c.add(b2);
        c.add(b3);
        c.add(b4);
        c.add(b5);
        c.add(b6);

        p1.setBounds(100, 10, 100, 40);
        p2.setBounds(100, 50, 100, 40);

        t1.setBounds(200, 10, 100, 30);
        t2.setBounds(200, 60, 100, 30);

        ta.setBounds(100, 100, 400, 30);
        ta1.setBounds(100, 200, 400, 30);

        b1.setBounds(320, 10, 80, 30);
        b2.setBounds(320, 60, 80, 30);
        b3.setBounds(100, 150, 90, 30);
        b4.setBounds(200, 150, 90, 30);
        b5.setBounds(300, 150, 90, 30);
        b6.setBounds(400, 150, 90, 30);

        addWindowListener(new WindowAdapter() {//点击窗口的“X”关闭
            @Override
            public void windowClosing(WindowEvent e1) {
                dispose();
                System.exit(0);
            }
        });

        this.setBounds(300, 50, 600, 300);
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Connection con;
        Statement sql;
        ResultSet rs;
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        } catch (ClassNotFoundException e1) {
            System.out.println(" " + e1);
        }//建立桥接器
        if ("确定".equals(e.getActionCommand())) {
            try {
                con = DriverManager.getConnection("jdbc:odbc:campus-card", "", "");//连接数据库;
                sql = con.createStatement();//向数据库发送SQL语句
                String no, pw;
                no = t1.getText();
                pw = t2.getText();
                rs = sql.executeQuery("SELECT * FROM message");//处理查询结果
                while (rs.next()) {
                    String sno = rs.getString(1);
                    String sname = rs.getString(2);
                    String pwd = rs.getString(3);
                    String type = rs.getString(4);
                    if (no.equals(sno) && pw.equals(pwd))//账号和密码与数据库的相符
                    {
                        ta.append("欢迎你：" + sname + "请选择您需要的操作！" + '\n');
                    }
                }
                con.close();
            } catch (SQLException e1) {
                System.out.println(e1);
            }
        } else if ("增加成员".equals(e.getActionCommand()))//增加成员按钮
        {
            try {
                con = DriverManager.getConnection("jdbc:odbc:campus-card", "", "");//连接数据库;
                sql = con.createStatement();
                String no = t1.getText();
                rs = sql.executeQuery("SELECT * FROM message  WHERE sno ='" + no + "'");//按学号查询
                if (rs.next()) {
                    String type = rs.getString(4);
                    //如果是学生就不能增加
                    if ("学生".equals(type)) {
                        ta1.append("对不起，您不是管理员，不能进行此项操作！" + '\n');
                    }
                    //管理员可以增加
                    else if ("管理员".equals(type)) {
                        new Insert().setVisible(true);
                        this.dispose();
                    }
                }else {
                    ta1.append("请先输入账户名密码才能进行此项操作！" + '\n');
                }
                con.close();
            } catch (SQLException e1) {
                System.out.println(e1);
            }
        }
        //删除成员按钮
        else if ("删除成员".equals(e.getActionCommand())) {
            try {
                con = DriverManager.getConnection("jdbc:odbc:campus-card", "", "");//连接数据库;
                sql = con.createStatement();
                String no = t1.getText();
                rs = sql.executeQuery("SELECT * FROM message  WHERE sno ='" + no + "'");//按学号查询
                if (rs.next()) {
                    String type = rs.getString(4);
                    //如果是学生就不能删除
                    if ("学生".equals(type)) {
                        ta1.append("对不起，您不是管理员，不能进行此项操作！" + '\n');
                    }
                    //管理员可以删除
                    else if ("管理员".equals(type)) {
                        new Delete().setVisible(true);
                        this.dispose();
                    }
                }else {
                    ta1.append("请先输入账户名密码才能进行此项操作！" + '\n');
                }
                con.close();
            } catch (SQLException e1) {
                System.out.println(e1);
            }
        }
        /*其他按钮的操作*/
        else if ("查询信息".equals(e.getActionCommand())) {
            new Search().setVisible(true);
            this.dispose();
        } else if ("修改密码".equals(e.getActionCommand())) {
            new Change().setVisible(true);
            this.dispose();
        } else if ("退出".equals(e.getActionCommand())) {
            this.dispose();
            System.exit(0);
        }
    }

    //主函数
    public static void main(String args[]) {
        new Load();
    }

}